/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author serga
 */
@Entity
@Table(name = "messages")
@XmlRootElement
@NamedQueries({
   // @NamedQuery(name = "Messages.findAll", query = "SELECT m FROM Messages m"),
    @NamedQuery(name = "Messages.findNewUserMessage", query = "SELECT m FROM Messages m WHERE m.isRead = :read and  m.idUserTo= :users"),
    @NamedQuery(name = "Messages.findUserDialog", query = "SELECT m FROM Messages m WHERE (m.idUserTo= :userTo and m.idUserFrom=:userFrom) or ((m.idUserFrom= :userTo and m.idUserFrom=:userTo)) ORDER BY m.dateTime"),
    @NamedQuery(name = "Messages.findByIsRead", query = "SELECT m FROM Messages m WHERE m.isRead = :isRead")})
public class Messages implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Basic(optional = false)
    @NotNull
    @Column(name = "id")
    private Long id;
    @Basic(optional = false)
    @NotNull
    @Lob
    @Size(min = 1, max = 65535)
    @Column(name = "text")
    private String text;
    @Column(name = "is_read")
    private Boolean isRead;
    @JoinColumn(name = "id_user_from", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Users idUserFrom;
    @JoinColumn(name = "id_user_to", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Users idUserTo;
    @Column(name = "date_time")
    @Temporal(TemporalType.DATE)
    private Date dateTime;

    public Date getDateTime() {
        return dateTime;
    }

    public void setDateTime(Date dateTime) {
        this.dateTime = dateTime;
    }
    
    public Messages() {
    }

    public Messages(Long id) {
        this.id = id;
    }

    public Messages(Long id, String text) {
        this.id = id;
        this.text = text;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Boolean getIsRead() {
        return isRead;
    }

    public void setIsRead(Boolean isRead) {
        this.isRead = isRead;
    }

    public Users getIdUserFrom() {
        return idUserFrom;
    }

    public void setIdUserFrom(Users idUserFrom) {
        this.idUserFrom = idUserFrom;
    }

    public Users getIdUserTo() {
        return idUserTo;
    }

    public void setIdUserTo(Users idUserTo) {
        this.idUserTo = idUserTo;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Messages)) {
            return false;
        }
        Messages other = (Messages) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "models.Messages[ id=" + id + " ]";
    }
    
}
